package Utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.LinkedList;

public class ConnectionPool {

    //静态的Connection队列
    private static LinkedList<Connection> connectionQueue;

    /**
     * 加载驱动
     */
    static {
        try {
            Class.forName("com.mysql,jdbc,Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取连接，多线程访问并发控制
     */
    public synchronized static Connection getConnection() throws SQLException {
        if (connectionQueue == null) {
            connectionQueue = new LinkedList<>();
            for (int i = 0; i < 10; i++) {
                Connection conn = DriverManager.getConnection(
                        "jdbc:mysql://spark1:3306/testdb",
                        "root",
                        "123456");
                connectionQueue.push(conn);
            }
        }
        return connectionQueue.poll();
    }

    /**
     * 还回去一个连接
     */
    public static void returnConnection(Connection conn) {
        connectionQueue.push(conn);
    }
}
